﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using University.Workshop.Model;

namespace University.Workshop.ServerApp
{
    /// <summary>
    /// Interaction logic for ActiveUsers.xaml
    /// </summary>
    public partial class ActiveUsers : Window
    {
        Collection<LogUser> users = new Collection<LogUser>();
        Collection<string> names = new Collection<string>();
        static TextBox t;
        TextChangedEventHandler newEvent;
        int Key;
        public ActiveUsers()
        {
            InitializeComponent();
        }
        public ActiveUsers(Collection<LogUser> log, TextBox box, Collection<string> Names)
        {
            InitializeComponent();
            users = log;
            t = box;
            newEvent = new TextChangedEventHandler(text_TextChanged);
            t.TextChanged += newEvent;
            names = Names;
            
            foreach (LogUser newUser in users)
            {
                
                listBox1.Items.Add(newUser.Name+"   "+newUser.Level+"    "+newUser.Now);
            }
        }

        private void text_TextChanged(object sender, TextChangedEventArgs e)
        {
            string []test = DateTime.Now.ToString().Split(' ');
            Key = test.Length;

            if (t.Text != " ")
            {
                Verify(t.Text.Split(' ')[Key+2], t.Text.Split(' ')[Key+3], t.Text.Split(' ')[Key], 0);
                
            }
        }
        private void Verify(string keyWord, string key, string name, int level)
        {

            if (keyWord == "Logged")
            {
                if (key == "on")
                {
                    names.Add(name);
                    LogUser log = new LogUser(name, level, DateTime.Now);
                    users.Add(log);
                    listBox1.Items.Add(log.Name + "   " + log.Level + "    " + log.Now);
                }
                else if (key == "out")
                {
                    int index = names.IndexOf(name);
                    if (index > -1)
                    {
                        names.Remove(name);
                        users.Remove(users[index]);
                        listBox1.Items.Remove(listBox1.Items[index]);
                    }
                }
            }

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            t.TextChanged -= newEvent;
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

    }
}
